package linter.controller.note;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DTO.UserDTO;
import linter.custom.ResultVO;
import linter.service.NoteService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "笔记卡片管理器")
@RestController
@RequestMapping("/noteCard")
public class NoteCardController {

    @Resource
    UserService userService;

    @Resource
    NoteService noteService;

    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }

    @PostMapping("/setNoteImgAndNoteTitleByUuid")
    @ApiOperation("查询所有的笔记卡片根据用户ID")
    public ResultVO<PageInfo<Object>> setNoteImgAndNoteTitleByUuid(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(noteService.setNoteImgAndNoteTitleByUuid(uuid, pageNum, pageSize));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
